package cn.edu.hnu.matching.config;

import cn.edu.hnu.matching.constants.AiConstants;
import com.alibaba.cloud.ai.advisor.DocumentRetrievalAdvisor;
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Spring AI Alibaba 相关配置，集成了阿里云百炼平台 RAG 检索增强功能
 */
@Configuration
public class SpringAiConfig {

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    @Bean
    public ChatClient chatClient(ChatClient.Builder builder) {
        String indexName = "伙伴星球知识库";

        DashScopeDocumentRetriever retriever = new DashScopeDocumentRetriever(
                new DashScopeApi(apiKey),
                DashScopeDocumentRetrieverOptions
                        .builder()
                        .withIndexName(indexName)
                        .build()
        );

        return builder
                .defaultSystem(AiConstants.PROMPT)
                // RAG 检索增强
                .defaultAdvisors(new DocumentRetrievalAdvisor(retriever))
                // TODO 对话记忆
                .build();
    }
}
